package com.agileai.crm.module.visit.handler;

import java.util.Date;

import com.agileai.crm.cxmodule.VisitManage;
import com.agileai.domain.DataParam;
import com.agileai.domain.DataRow;
import com.agileai.hotweb.annotation.PageAction;
import com.agileai.hotweb.bizmoduler.core.StandardService;
import com.agileai.hotweb.controller.core.StandardEditHandler;
import com.agileai.hotweb.domain.FormSelectFactory;
import com.agileai.hotweb.domain.core.User;
import com.agileai.hotweb.renders.LocalRenderer;
import com.agileai.hotweb.renders.RedirectRenderer;
import com.agileai.hotweb.renders.ViewRenderer;
import com.agileai.util.DateUtil;

/**
 * generated by miscdp
 */
public class MyTasksVisitCreateClueEditHandler extends StandardEditHandler{
	public MyTasksVisitCreateClueEditHandler(){
		super();
		this.listHandlerClass = MyTasksVisitManageListHandler.class;
		this.serviceId = buildServiceId(VisitManage.class);
	}
	
	public ViewRenderer prepareDisplay(DataParam param) {
		String operaType = param.get(OperaType.KEY);
		User user = (User) this.getUser();
		if ("doCreateClueAction".equals(operaType)||"doCreateClueAction,detail".equals(operaType)){
			DataRow record = getService().getRecord(param);
			DataParam showParam = new DataParam();
			showParam.put("CUST_ID", record.get("VISIT_CUST_ID"));
			showParam.put("OPP_CREATE_TIME", DateUtil.getDateByType(
					DateUtil.YYMMDDHHMISS_HORIZONTAL, new Date()));
			showParam.put("OPP_CREATER", user.getUserId());
			setAttribute("OPP_CREATER_NAME",user.getUserName());
			record.putAll(showParam);
			this.setAttributes(record);	
		}
		this.setOperaType(operaType);
		processPageAttributes(param);
		return new LocalRenderer(getPage());
	}
	
	@PageAction
	public ViewRenderer createClue(DataParam param) {
		((VisitManage) getService()).createClueRecord(param);
		return new RedirectRenderer(getHandlerURL(listHandlerClass));
	}
	
	protected void processPageAttributes(DataParam param) {
	setAttribute("OPP_LEVEL", FormSelectFactory.create("OPP_LEVEL")
			.addSelectedValue(getOperaAttributeValue("OPP_LEVEL", "")));	
	setAttribute("CLUE_SOURCE", FormSelectFactory.create("CLUE_SOURCE")
			.addSelectedValue(getOperaAttributeValue("CLUE_SOURCE", "")));
	setAttribute("OPP_STATE",
            FormSelectFactory.create("OPP_STATE")
                             .addSelectedValue(getAttributeValue("OPP_STATE",
                                                                      "0")));
	}
	
	protected StandardService getService() {
		return (StandardService) this.lookupService(this.getServiceId());
	}
	
	public ViewRenderer doBackAction(DataParam param) {
		return new RedirectRenderer(getHandlerURL(listHandlerClass)+"&custId="+param.get("CUST_ID"));
	}
}
